package com.jushu.storbox.fragment;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.jushu.storbox.R;
import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.actions.FileFolderAction;
import com.jushu.storbox.adapter.MoveAdapter;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.service.ICloudDriveService;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.view.pulltorefresh.MyScrollView;
import com.huawei.sharedrive.sdk.android.exception.ClientException;

public class MoveFragment extends BaseFragment {

	/** ShareFragment TAG */
	public static final String TAG = "MoveFragment";
	/** activity */
	private Activity activity;
	/** activity handler */
	private Handler activityHandler;
	/** cloudriverService */
	private ICloudDriveService shareDriveService;
	/** move Entity */
	private FileFolderInfo moveInfo;
	/** move parent */
	private FileFolderInfo parentInfo;
	private FileFolderInfo exceptionInfo;
	/** contentView */
	private View contentView;
	/** ListView */
	private ListView FileList_lv;
	/** canccle_btn */
	private List<FileFolderInfo> list;
	private int index = 0;
	private MyScrollView moveScr;
	private LinearLayout moveLL;
	private FileFolderAction actions;
	private String returnParent = "move";

	/**
	 * onAttach
	 * 
	 * @param activity
	 *            Activity instance
	 */
	@Override
	public void onAttach(Activity activity) {
		LogUtil.i(TAG, "onAttach Activity");
		this.activity = activity;
		setBack(false);
		if (null == getObj()) {
			System.exit(1);
		} else {
			Map<String, Object> mp = (Map<String, Object>) getObj();
			if (null != mp.get("activityhandler")) {
				activityHandler = (Handler) mp.get("activityhandler");
			} else {
				PublicTools.restartApp(activity);
			}
			if (null != mp.get("service")) {
				shareDriveService = (ICloudDriveService) mp.get("service");
			} else {
				PublicTools.restartApp(activity);
			}
			moveInfo = (FileFolderInfo) mp.get("except_entity");
			parentInfo = (FileFolderInfo) mp.get("move_entity");
			if (null != parentInfo) {
				returnParent = parentInfo.getName();
			}
			index = (Integer) mp.get("index");
			actions = new FileFolderAction(activity);
			actions.setSuccessHandler(activityHandler);
			actions.setExceptionHandler(activityHandler);
		}
		super.onAttach(activity);
	}

	/**
	 * onCreate in Fragment
	 * 
	 * @param savedInstanceState
	 *            Bundle
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		LogUtil.i(TAG, "onCreate()");
		super.onCreate(savedInstanceState);
	}

	/**
	 * onCreateView in Fragment
	 * 
	 * @param inflater
	 *            LayoutInflater
	 * @param container
	 *            ViewGroup
	 * @param savedInstanceState
	 *            Bundle
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		LogUtil.i(TAG, "onCreateView method");
		contentView = LayoutInflater.from(getActivity()).inflate(
				R.layout.window_move, null);
		initFindViewById();
		initData();
		initListenser();
		return contentView;
	}

	public void moveConfirm() {
		new Thread(new Runnable() {

			@Override
			public void run() {

				String itemId = null;
				String oldParentId = "0";
				String moveCurrentParenId = "0";
				if (null != moveInfo) {
					itemId = moveInfo.getId();
					if(null != moveInfo.getParent()){
						oldParentId = moveInfo.getParent();
					}
				} else {
					return;
				}
				if (null != parentInfo) {
					moveCurrentParenId = parentInfo.getId();
				}
				if (moveInfo.getIsFile() == 1) {
					if (null != shareDriveService) {
						actions.removeFile(shareDriveService,
								moveInfo, parentInfo);
					}
					
				} else {
					if (null != shareDriveService) {
						actions.removeFolder(shareDriveService,
								moveInfo, parentInfo);
					}
				}

			}
		}).start();
	}

	public void moveCancle() {
		activityHandler.sendEmptyMessage(8);
	}

	private void initListenser() {
	}

	private void initData() {
		moveScr.setLayout(moveLL);
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					// TODO Auto-generated method stub
					String folderId = "0";
					if (1 == index) {
					} else {
						if (null != parentInfo) {
							folderId = parentInfo.getId();
						}
					}
					list = getMoveFolderList(ShareDriveApplication.getInstance().getWnerID(), folderId, moveInfo.getId());
					Message msg = new Message();
					msg.what = 0;
					msg.obj = list;
					handler.sendMessageDelayed(msg,310);
				} catch (ClientException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}

	private void initFindViewById() {
		FileList_lv = (ListView) contentView.findViewById(R.id.FileList_lv);
		moveScr = (MyScrollView) contentView.findViewById(R.id.move_scrollview);
		moveLL = (LinearLayout) contentView.findViewById(R.id.move_layout);
	}

	/**
	 * onActivityCreated in Fragment
	 * 
	 * @param savedInstanceState
	 *            Bundle
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		LogUtil.i(TAG, "onActivityCreated()");
		super.onActivityCreated(savedInstanceState);
	}

	/**
	 * onStart in Fragment
	 */
	@Override
	public void onStart() {
		LogUtil.i(TAG, "onStart()");
		super.onStart();
	}

	/**
	 * onResume in Fragment
	 */
	@Override
	public void onResume() {
		LogUtil.i(TAG, "onResume()");
		super.onResume();
	}

	/**
	 * onPause in Fragment
	 */
	@Override
	public void onPause() {
		LogUtil.i(TAG, "onResume()");
		super.onPause();
	}

	/**
	 * onSaveInstanceState in Fragment
	 * 
	 * @param savedInstanceState
	 *            Bundle
	 */
	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		LogUtil.i(TAG, "onSaveInstanceState()");
		super.onSaveInstanceState(savedInstanceState);
	}

	/**
	 * onStop in Fragment
	 */
	@Override
	public void onStop() {
		LogUtil.i(TAG, "onStop()");
		super.onStop();
	}

	/**
	 * onDestroy in Fragment
	 */
	@Override
	public void onDestroy() {
		LogUtil.i(TAG, "onDestroy()");
		super.onDestroy();
	}

	/**
	 * onDestroyView in Fragment
	 */
	@Override
	public void onDestroyView() {
		LogUtil.i(TAG, "onDestroyView()");
		super.onDestroyView();
	}

	/**
	 * onDetach in Fragment
	 */
	@Override
	public void onDetach() {
		LogUtil.i(TAG, "onDetach()");
		super.onDetach();
	}

	private void showMsgData(List<FileFolderInfo> shareResource, int msgDir) {
		setBack(true);
	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				moveScr.removeAllItem();
				List<FileFolderInfo> list = (List<FileFolderInfo>) msg.obj;
				MoveAdapter adapter = new MoveAdapter(activity, handler, list);
				moveScr.setAdptor(adapter);
				moveScr.notifyAllItem();
				setBack(true);
				break;
			case 1:
				list = null;
				Message moveMsg = new Message();
				moveMsg.obj = msg.obj;
				moveMsg.what = 2;
				moveMsg.arg1 = 0;
				activityHandler.sendMessage(moveMsg);
				break;
			default:
				break;
			}
		};
	};

	private List<FileFolderInfo> getMoveFolderList(String ownerId, String folderId,
			String exception) throws ClientException {
		List<FileFolderInfo> moveList = null;
		if (null != shareDriveService) {
			moveList = shareDriveService.getMoveFolderListDB(ownerId, folderId,
					exception);
		}
		return moveList;

	}

	@Override
	public void refreshListFromServer() {
		// TODO Auto-generated method stub

	}

	@Override
	public String getParent() {
		// TODO Auto-generated method stub
		return returnParent;
	}
	@Override
	public void onDataLoaded() {
		// TODO Auto-generated method stub
		if (null != activityHandler) {
			Message dissMissMsg = new Message();
			dissMissMsg.what = 12;
			activityHandler.sendMessageDelayed(dissMissMsg, 100);
		}
	}
}
